import axios from "axios";

const pre = localStorage.getItem("url_prefix")

if (process.env.NODE_ENV == 'development') {
    axios.defaults.baseURL = 'http://localhost:50000/api'
} else if (pre) {
    axios.defaults.baseURL = pre + '/api'
} else {
    axios.defaults.baseURL = '/api'
}


function addToken(url) {
    let _token = localStorage.getItem("local:_token");
    let back = url;

    if (back.indexOf("?") != -1) {
        back = back + "&_token=" + _token;
    } else {
        back = back + "?_token=" + _token;
    }

    return back;
}

function post(url, param = {}) {
    url = addToken(url);
    return axios.post(url, param);
}

function get(url, param = {}) {
    url = addToken(url);
    return axios({
        method: "get",
        url: url,
        params: param,
        responseType: "json"
    })
}

export { addToken, get, post };

